package com.ruoyi.system.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.system.domain.BaseCategroy;
import com.ruoyi.system.domain.BaseGoods;
import com.ruoyi.system.domain.SupplyGoods;
import com.ruoyi.system.mapper.BaseCategroyMapper;
import com.ruoyi.system.mapper.BaseGoodsMapper;
import com.ruoyi.system.mapper.SupplyGoodsMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.SupplyCategroyMapper;
import com.ruoyi.system.domain.SupplyCategroy;
import com.ruoyi.system.service.ISupplyCategroyService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 基础分类Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-08-19
 */
@Service
public class SupplyCategroyServiceImpl implements ISupplyCategroyService 
{
    @Autowired
    private SupplyCategroyMapper supplyCategroyMapper;
    @Autowired
    private SupplyGoodsMapper supplyGoodsMapper;
    @Autowired
    private BaseCategroyMapper baseCategroyMapper;
    @Autowired
    private BaseGoodsMapper baseGoodsMapper;
    /**
     * 将基础商品信息同步给该供货商
     *
     * @param supplierId
     * @return
     */
    @Override
    @Transactional
    public AjaxResult syncGoods(Long supplierId) {

        //删除该供应商的全部分类，商品信息
        supplyCategroyMapper.deleteBySupply(supplierId);
        supplyGoodsMapper.deleteBySupply(supplierId);
        //插入该供应商的全部分类，商品信息
        //处理分类
        List<SupplyCategroy> supplyCategroyList = new ArrayList<>();
        List<BaseCategroy> baseCategroyList = baseCategroyMapper.selectBaseCategroyList(new BaseCategroy());
        for (BaseCategroy baseCategroy : baseCategroyList) {
            SupplyCategroy supplyCategroy = new SupplyCategroy();
            supplyCategroy.setId(baseCategroy.getId()+"_"+supplierId);
            supplyCategroy.setSupplyId(supplierId);
            supplyCategroy.setSort(baseCategroy.getSort());
            supplyCategroy.setFid(baseCategroy.getFid()+"_"+supplierId);
            supplyCategroy.setName(baseCategroy.getName());
            supplyCategroy.setLevel(baseCategroy.getLevel());
            supplyCategroyList.add(supplyCategroy);
        }
        supplyCategroyMapper.insertMulity(supplyCategroyList);
        //处理商品
        List<SupplyGoods> supplyGoodsList = new ArrayList<>();
        List<BaseGoods> baseGoodsList = baseGoodsMapper.selectBaseGoodsList(new BaseGoods());
        for (BaseGoods baseGoods : baseGoodsList) {
            SupplyGoods supplyGoods = new SupplyGoods();
            supplyGoods.setSupplyId(supplierId);
            supplyGoods.setCategroyId(baseGoods.getCategroyId()+"_"+supplierId);
            supplyGoods.setSort(baseGoods.getSort());
            supplyGoods.setName(baseGoods.getName());
            supplyGoods.setCategroyName(baseGoods.getCategroyName());
            supplyGoods.setPrice(baseGoods.getPrice());
            supplyGoods.setCode(baseGoods.getCode());
            supplyGoods.setCunit(baseGoods.getCunit());
            supplyGoods.setLife(baseGoods.getLife());
            supplyGoods.setImageUrl(baseGoods.getImageUrl());
            supplyGoods.setUnit(baseGoods.getUnit());

            supplyGoodsList.add(supplyGoods);
        }
        supplyGoodsMapper.insertMulity(supplyGoodsList);

        return AjaxResult.success();
    }

    /**
     * 查询分类数据，返回JSON格式
     *
     * @param supplyCategroy
     * @return
     */
    @Override
    public AjaxResult getJsonData(SupplyCategroy supplyCategroy) {
        JSONArray jsa = new JSONArray();
        List<SupplyCategroy> list = selectSupplyCategroyList(supplyCategroy);
        //将所有数据转入JSA中
        if (list != null && list.size() > 0) {
            for (SupplyCategroy scate : list) {
                JSONObject scate_json = scate.toJSONObject();
                jsa.add(scate_json);
                //如果不是一级分类，则添加到父级的children中
                if(scate.getLevel()>1){
                    for(int i=0;i<jsa.size();i++){
                        JSONObject jso = jsa.getJSONObject(i);
                        if(jso.get("id").equals(scate.getFid())){
                            if(!jso.containsKey("children")){
                                jso.put("children", new JSONArray());
                            }
                            jso.getJSONArray("children").add(scate_json);
                        }
                    }
                }
            }
        }

        //将所有1级数据装入JSONArray中
        JSONArray jsa1 = new JSONArray();
        for (int i = 0; i < jsa.size(); i++) {
            JSONObject jso = jsa.getJSONObject(i);
            if (jso.get("level").equals(1)) {
                jsa1.add(jso);
            }
        }
        //返回JSON格式数据
        AjaxResult ar = AjaxResult.success();
        ar.put("data", jsa1);
        return ar;
    }

    /**
     * 查询基础分类
     * 
     * @param id 基础分类主键
     * @return 基础分类
     */
    @Override
    public SupplyCategroy selectSupplyCategroyById(String id)
    {
        return supplyCategroyMapper.selectSupplyCategroyById(id);
    }

    /**
     * 查询基础分类列表
     * 
     * @param supplyCategroy 基础分类
     * @return 基础分类
     */
    @Override
    public List<SupplyCategroy> selectSupplyCategroyList(SupplyCategroy supplyCategroy)
    {
        return supplyCategroyMapper.selectSupplyCategroyList(supplyCategroy);
    }

    /**
     * 新增基础分类
     * 
     * @param supplyCategroy 基础分类
     * @return 结果
     */
    @Override
    public int insertSupplyCategroy(SupplyCategroy supplyCategroy)
    {
        return supplyCategroyMapper.insertSupplyCategroy(supplyCategroy);
    }

    /**
     * 修改基础分类
     * 
     * @param supplyCategroy 基础分类
     * @return 结果
     */
    @Override
    public int updateSupplyCategroy(SupplyCategroy supplyCategroy)
    {
        return supplyCategroyMapper.updateSupplyCategroy(supplyCategroy);
    }

    /**
     * 批量删除基础分类
     * 
     * @param ids 需要删除的基础分类主键
     * @return 结果
     */
    @Override
    public int deleteSupplyCategroyByIds(String[] ids)
    {
        return supplyCategroyMapper.deleteSupplyCategroyByIds(ids);
    }

    /**
     * 删除基础分类信息
     * 
     * @param id 基础分类主键
     * @return 结果
     */
    @Override
    public int deleteSupplyCategroyById(String id)
    {
        return supplyCategroyMapper.deleteSupplyCategroyById(id);
    }
}
